package com.demo.servicelmpl;

import com.demo.bean.ConflictObjects;
import com.demo.bean.FixedEvent;
import com.demo.bean.FourNums;
import com.demo.mapper.FixedEventsMapper;
import com.demo.service.FixedEventService;
import com.demo.service.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author 李天航
 * 操作活动的实现类
 */
@Service
public class FixedEventServiceImpl implements FixedEventService {
    @Resource
    FixedEventsMapper fixedEventsMapper;
    @Resource
    RedisService redisService;

    public static int MAX_DAILY_ACTIVITY_NUM =10;

    private ConflictObjects judgeConflict(FixedEvent fixedEvent)
    {
        FixedEvent fixedEvent1=fixedEventsMapper.firstLargerBeginTime(fixedEvent);
        if(fixedEvent1!=null&&fixedEvent.getEventName()!=null&&fixedEvent.getEndTime()>fixedEvent1.getBeginTime()) {
            return new ConflictObjects(false,fixedEvent1,fixedEvent);
        }
        FixedEvent fixedEvent2=fixedEventsMapper.firstLargerEndTime(fixedEvent);
        if(fixedEvent2!=null&&fixedEvent.getBeginTime()!=null&&fixedEvent2.getBeginTime()<fixedEvent.getEndTime()) {
            return new ConflictObjects(false,fixedEvent2,fixedEvent);
        }
        return new ConflictObjects(true,null,null);
    }


    @Override
    public LinkedHashMap<String, Object> addFixedEvent(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(fixedEvent.getBeginTime()> fixedEvent.getEndTime())
            {
                resultMap.put("status",500);
                resultMap.put("msg","起始时间不能大于终止时间");
                return resultMap;
            }
            ConflictObjects conflictObjects=judgeConflict(fixedEvent);
            if(!conflictObjects.isNotConflict)
            {
                resultMap.put("status",500);
                resultMap.put("msg","活动与其他活动冲突");
                resultMap.put("addFixedEvent",conflictObjects.rawObject);
                resultMap.put("conflictFixedEvent",conflictObjects.newObject);
            }
            else
            {
                if(fixedEventsMapper.getFixedEventCount(fixedEvent)<MAX_DAILY_ACTIVITY_NUM) {
                    fixedEventsMapper.addFixedEvent(fixedEvent);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "添加成功");
                }
                else
                {
                    resultMap.put("status", 500);
                    resultMap.put("msg", "数量达到上限");
                }

            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> deleteFixedEvent(Integer fixedEventId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            FixedEvent fixedEvent=fixedEventsMapper.getFixedEventById(fixedEventId);
            if(!fixedEvent.getUserAccount().equals(userAccount))
            {
                resultMap.put("status",500);
                resultMap.put("msg","无修改权限");
            }
            else {
                fixedEventsMapper.deleteFixedEventById(fixedEventId);
                resultMap.put("status", 200);
                resultMap.put("msg", "删除成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> updateFixedEvent(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            FixedEvent rawFixedEvent=fixedEventsMapper.getFixedEventById(fixedEvent.getEventId());
            if(fixedEvent.getUserAccount().equals(rawFixedEvent.getUserAccount())) {
                if(fixedEvent.getBeginTime()!=null) {
                    rawFixedEvent.setBeginTime(fixedEvent.getBeginTime());
                }
                if(fixedEvent.getEndTime()!=null) {
                    rawFixedEvent.setEndTime(fixedEvent.getEndTime());
                }
                if(rawFixedEvent.getBeginTime()> rawFixedEvent.getEndTime())
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","起始时间不能大于终止时间");
                    return resultMap;
                }
                ConflictObjects conflictObjects=judgeConflict(rawFixedEvent);
                if(conflictObjects.isNotConflict) {
                    fixedEventsMapper.updateFixedEvent(fixedEvent);
                    resultMap.put("status", 200);
                    resultMap.put("msg", "修改成功");
                }
                else
                {
                    resultMap.put("status",500);
                    resultMap.put("msg","活动与其他活动冲突");
                    resultMap.put("updateFixedEvent",conflictObjects.rawObject);
                    resultMap.put("conflictFixedEvent",conflictObjects.newObject);
                }
            }
            else
            {
                resultMap.put("status", 500);
                resultMap.put("msg", "无修改权限");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getFixedEvents(FixedEvent fixedEvent) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("fixedEvents",fixedEventsMapper.getFixedEventsByUserAccountAndWeekNum(fixedEvent));
            resultMap.put("status",200);
            resultMap.put("msg","查看成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
}
